﻿//-----------------------------------------------------------------------
// <copyright file="ProjectsListViewModel.cs" author="Artak Mkrtchyan">
//     Creative Commons - GNU General Public License (http://creativecommons.org/licenses/GPL/2.0/
// </copyright>
// <author>Artak Mkrtchyan</author>
// <email>mkArtak@gmail.com</email>
// <date>24/04/2013</date>
//-----------------------------------------------------------------------
using ConsoleHoster.Common.Notifications;
using ConsoleHoster.Common.Utilities;
using ConsoleHoster.Common.Utilities.Threading;
using ConsoleHoster.Common.ViewModel;
using System.Collections.Generic;
using System.Collections.ObjectModel;

namespace ConsoleHoster.ViewModel
{
	public class ProjectsListViewModel : ViewModelBase
	{
		private readonly IUserInteractionManager userInteractionManager;
		private readonly ObservableCollection<ProjectDetailsViewModel> projects;
		private ProjectDetailsViewModel selectedProject;

		public ProjectsListViewModel(IEnumerable<ProjectDetailsViewModel> argProjects, IUserInteractionManager argInteractionManager, IDispatcher argDispatcher, ILogger argLogger)
			: base(argDispatcher, new ComponentLogger("ProjectsListViewModel", argLogger))
		{
			ValidationManager.RequireArgumentNotNull(argInteractionManager, "argInteractionManager");

			this.projects = new ObservableCollection<ProjectDetailsViewModel>(argProjects);
			this.selectedProject = null;
			this.userInteractionManager = argInteractionManager;
		}

		public void RemoveProject(ProjectDetailsViewModel argProject)
		{
			if (!this.userInteractionManager.Confirm(StringUtilities.Format("Do you really want to remove the project: '{0}'.", argProject.Project.Name), "Confirm delete"))
			{
				return;
			}

			this.Projects.Remove(argProject);
			if (this.SelectedProject == argProject)
			{
				this.SelectedProject = null;
			}
		}

		public ProjectDetailsViewModel SelectedProject
		{
			get
			{
				return this.selectedProject;
			}
			set
			{
				this.SetPropertyValue<ProjectDetailsViewModel>("SelectedProject", ref this.selectedProject, value);
			}
		}

		public ObservableCollection<ProjectDetailsViewModel> Projects
		{
			get
			{
				return this.projects;
			}
		}
	}
}
